<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="冷库名称" prop="storageName">
      <el-input v-model="dataForm.storageName" placeholder="冷库名称"></el-input>
    </el-form-item>
    <el-form-item label="冷库地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="冷库地址"></el-input>
    </el-form-item>
    <el-form-item label="冷库长度" prop="length">
      <el-input type="number" v-model="dataForm.length" placeholder="冷库长度" min="0" :step="0.1"></el-input>
    </el-form-item>
    <el-form-item label="冷库宽度" prop="width">
      <el-input type="number" v-model="dataForm.width" placeholder="冷库宽度" min="0" :step="0.1"></el-input>
    </el-form-item>
    <el-form-item label="冷库高度" prop="height">
      <el-input type="number" v-model="dataForm.height" placeholder="冷库高度" min="0" :step="0.1"></el-input>
    </el-form-item>
    <el-form-item label="可用面积" prop="totalSpace">
      <el-input v-model="dataForm.totalSpace" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="已用面积" prop="usedSpace">
      <el-input v-model="dataForm.usedSpace" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="冷库类型" prop="storageType">
      <el-select v-model="dataForm.storageType" placeholder="请选择">
        <el-option
          v-for="item in storageOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="商品类型" prop="goodsType">
      <el-select v-model="dataForm.goodsType" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="冷库图片" prop="storagePic">
      <el-upload
        class="avatar-uploader"
        :action="url"
        :show-file-list="false"
        :on-success="successHandle"
        :before-upload="beforeUploadHandle">
        <img v-if="dataForm.storagePic" :src="dataForm.storagePic" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        options: [],
        storageOptions: [],
        imageUrl: '',
        dataForm: {
          id: 0,
          storageName: '',
          storageType: '',
          address: '',
          length: '',
          width: '',
          height: '',
          goodsType: '',
          storagePic: '',
          totalSpace: '',
          usedSpace: ''
        },
        dataRule: {
          storageName: [
            { required: true, message: '冷库名称不能为空', trigger: 'blur' }
          ],
          storageType: [
            { required: true, message: '冷库类型不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '冷库地址不能为空', trigger: 'blur' }
          ],
          length: [
            { required: true, message: '冷库长度不能为空', trigger: 'blur' }
          ],
          width: [
            { required: true, message: '冷库宽度不能为空', trigger: 'blur' }
          ],
          height: [
            { required: true, message: '冷库高度不能为空', trigger: 'blur' }
          ],
          goodsType: [
            { required: true, message: '商品类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.url = this.$http.adornUrl(`/sys/minio/upload?token=${this.$cookie.get('Ent-token')}`)
    },
    methods: {
      successHandle (response, res, file) {
        this.imageUrl = window.URL.createObjectURL(res.raw)
        if (response && response.code === 0) {
          if (this.num === this.successNum) {
            this.dataForm.storagePic = response.url
            this.$message.info('上传成功')
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      beforeUploadHandle (file) {
        const isLt2M = file.size / 1024 / 1024 < 2
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
          return false
        }
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entcoldstorage/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                console.log("dayin", data)
                this.dataForm.storageName = data.entColdStorage.storageName
                this.dataForm.storageType = data.entColdStorage.storageType
                this.dataForm.address = data.entColdStorage.address
                this.dataForm.length = data.entColdStorage.length
                this.dataForm.width = data.entColdStorage.width
                this.dataForm.height = data.entColdStorage.height
                this.dataForm.goodsType = data.entColdStorage.goodsType
                this.dataForm.storagePic = data.entColdStorage.storagePic
                this.dataForm.entId = data.entColdStorage.entId
                this.dataForm.totalSpace = data.entColdStorage.totalSpace
                this.dataForm.usedSpace = data.entColdStorage.usedSpace
              }
            })
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/app/dictitem/getListById/commodity_type`),
          method: "get",
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.options = data.list
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/app/dictitem/getListById/storage_type`),
          method: "get",
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.storageOptions = data.list
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entcoldstorage/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'storageName': this.dataForm.storageName,
                'storageType': this.dataForm.storageType,
                'address': this.dataForm.address,
                'length': this.dataForm.length,
                'width': this.dataForm.width,
                'height': this.dataForm.height,
                'goodsType': this.dataForm.goodsType,
                'storagePic': this.dataForm.storagePic
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
